import game.buff.AttackDefenseScaleBuff;
import game.worlds.GameRole;
import game.buff.AttackScaleBuff;
import hx.geom.Point;
import game.worlds.GameEffect;
import game.event.RoleEvent;
import game.script.RoleScript;
import Math;
import game.buff.SpeedScaleBuff;

class Script extends RoleScript {
	static function main() {}

	override function onInit() {
		super.onInit();
		// 代码入口位置
		this.role.addEventListener(RoleEvent.FRAME_ENTER, onRoleEvent);
		// 侦听角色攻击成功事件
		this.role.addEventListener(RoleEvent.ATTACKED, onAttacked);
		// 初始化测试移速buff
		var speedBuff = new SpeedScaleBuff("test_speed_buff", 5);
		// 增加50%移动速度
		speedBuff.value = 1.5;
		this.role.buffData.addBuff(speedBuff);
	}

	/**
	 * 攻击成功时
	 * @param e 
	 */
	public function onAttacked(e:RoleEvent) {
		if (e.hurtData.event == "wingHit")
			addWingBuff(e.hurtData.role);
	}

	/**
	 * 为敌人添加风伤BUFF
	 * @param role 
	 */
	public function addWingBuff(enemy:GameRole):Void {
		// 添加BUFF，持续5秒
		var buff = new AttackDefenseScaleBuff("jianxin_wing", 5);
		// 削弱敌人50%的物理防御力
		buff.value = 0.5;
		// 创建BUFF渲染动画
		var effect = new GameEffect(this.role, "build4");
		buff.renderDisplay = effect;
		effect.followTarget = enemy;
		effect.followOffsetPoint = new Point(0, -enemy.height / 2 + 10);
		effect.scaleX = effect.scaleY = 0.5;
		effect.liveTime = 0;
		effect.blendMode = "add";
		effect.alpha = 0.5;
		// 添加BUFF
		enemy.buffData.addBuff(buff);
	}

	public function onRoleEvent(e:RoleEvent):Void {
		// 到了动作的第三帧开始调用
		switch (e.action) {
			case "龙卷闪·岚":
				if (e.frame == 6) {
					if (role.jumping) {
						this.role.goToFrame(3);
					} else {
						this.role.goToFrame(7);
					}
				} else if (!this.role.jumping && e.frame > 3 && e.frame < 7) {
					this.role.goToFrame(7);
				}
			case "龍卷閃˙空":
				if (e.frame == 6) {
					if (this.role.jumping) {
						this.role.goToFrame(3);
					} else {
						this.role.playSkill("龙卷闪·落");
					}
				} else {
					role.move(2 * this.role.scaleX, 0);
				}
			case "破军式":
				// 判断附近的敌人距离，小于80px，则直接跳转
				if (e.frame < 3) {
					if (this.role.getEnemys().filter((f) -> {
						Point.distanceByFloat(f.x, f.y, this.role.x, this.role.y) < 80;
					}).length >= 1) {
						this.role.goToFrame(3);
					}
				}
			case "快速后移":
				if (e.frame == 4) {
					if (this.role.getEnemys().filter((f) -> {
						Point.distanceByFloat(f.x, f.y, this.role.x, this.role.y) < 140;
					}).length >= 1) {
						this.role.playSkill("后跳斩");
					}
				}
		}
	}
}